<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;
//引用对应的命名空间
use Gregwar\Captcha\CaptchaBuilder;
use Session;

class IndexController extends Controller
{   
    //前台主页
    public function index(){
        //查询友情链接数据

        $flinks = DB::table('flinks')->get();
        $goods = DB::table('goods')->take(3)->get();
        return view('/home/index',['flinks'=>$flinks,'goods'=>$goods]);
   }

    //订单页面
    public function order(Request $request){
        //查询友情链接数据
        $flinks = DB::table('flinks')->get();
        return view('/home/order',['flinks'=>$flinks]);
    }

    //注册页面
    public function register(){
        return view('/home/register');
    }

    //添加用户操作
    public function insert(Request $request){
        //验证 验证码是否正确
        $userInput = \Request::get('captcha');

        if (Session::get('milkcaptcha') != $userInput) {
            //用户输入验证码错误
            return error('/home/register','输入验证码错误',2);
        } 

        //dd($request);
        if($request['uname']=='' || $request['upwd']=='' || $request['uemail']==''){
            return error('/home/register','您填写的信息不完整',2);
        }

        //验证两次密码是否一致
        //var_dump($request->input('upwd'));
        //var_dump($request->input('repwd'));
        
        if($request->input('upwd') != $request->input('repwd')){
            return error('/home/register','两次密码不一致',2);
        }else{
            //获取数据
            $user = $request->only('uname','upwd','uemail');
            $user['upwd'] = md5($user['upwd']);
            //将数据插入数据库
            $result = DB::table('users')->insert($user);

            if($result){
                //接收表单信息
                $uname = $request->input('uname');
                //dd($request);
                $upwd = md5($request->input('upwd'));

                //查询用户信息
                $result = DB::table('users')
                                ->where('uname','=',$uname)
                                ->where('upwd','=',$upwd)
                                ->select('uid','uname','uemail')
                                ->first();
                //验证用户是否存在和密码是否正确
                //var_dump($result);
                if($result){
                    //查询到用户  写入session信息并跳转到后台首页
                    $request->session()->put('isHome',true);//登录标志
                    $request->session()->put('uid',$result['uid']);//用户ID
                    $request->session()->put('uname',$result['uname']);//用户名
                    $request->session()->put('uemail',$result['uemail']);//用户邮箱

                    //跳转到前台首页
                    return redirect('/home/index');
                }else{
                    //没查询到用户  回登陆页面
                    return redirect('/admin/login');
                }
            }
        }
    }

    //购物车页面
    public function mycart(){
        return view('/home/mycart');
    }

    //男士上装页面
    public function nans(){
        return view('/home/list/nans');
    }

}
